package Dao;

import model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;

@Repository
//该注解用于将数据访问层（DAO）的类标识为Bean，即注解数据访问层Bean，其功能与@Component相同。

public class StudentDaoImpl implements StudentDao {


    private JdbcTemplate jdbcTemplate;

    private List<Student> students;

    @Override
    public void insertStudent(Student student) {

        String INSERT_Student = "INSERT INTO student(id,name,sex,grades) VALUES(?,?,?,?)";

        Object[] args = {student.getId(), student.getName(), student.getSex(),student.getGrades()};
        jdbcTemplate.update(INSERT_Student, args);

        }

    @Override
    public List<Student> queryAll() {
        String QUERY_ALL_Student = "select * from student";
        RowMapper<Student> rowMapper=new BeanPropertyRowMapper<Student>(Student.class);//行映射
        students = jdbcTemplate.query(QUERY_ALL_Student,rowMapper);
        return students;
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Resource(name="list")
    public void setStudents(List<Student> students) {
        this.students = students;
    }
}
